import requests
import json

app_id = "71856c27-2f40-41b5-b1cb-0cedd6018d52"
auth =  'Bearer bce-v3/ALTAK-hQuQHFu0oD6y9VwjTOhxq/98094bc3258e03b722eb860ea3ce27caa715102a'

#1.新建会话，得到conversation_id

url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({
    "app_id": app_id
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': auth
}

response = requests.request("POST", url, headers=headers, data=payload)


data = json.loads(response.text)
conversation_id = data["conversation_id"]

#步骤1,2之间加出来的
url = "https://qianfan.baidubce.com/v2/app/conversation/file/upload"

payload = {
    'app_id': app_id,
    'conversation_id': conversation_id
}
files = [
    ('file',('111.jpg',open('C:\\Users\\zh415st028\\Desktop\\111.jpg','rb'),'image/jpeg'))
]
headers = {
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-hQuQHFu0oD6y9VwjTOhxq/98094bc3258e03b722eb860ea3ce27caa715102a'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

file_id = json.loads(response.text)['id']

#2.发送请求，得到AppBuilder的结果

url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
    "app_id": app_id,
    "query": input("请输入问题："),
    "stream": False,
    "conversation_id": conversation_id,
    "file_ids": [
        file_id
    ]
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': auth
}

response = requests.request("POST", url, headers=headers, data=payload)

#print(response.text)


data = json.loads(response.text)
answer = data["answer"]


print(answer)

